home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / mmapfile / MMAPFILE.ZIP / UMEMF1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-06-22  |  3.2 KB  |  138 lines

  1. unit UMemF1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   E_MemMap, Buttons, StdCtrls, Mask, ComCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     SpeedButton1: TSpeedButton;
  12.     SpeedButton2: TSpeedButton;
  13.     Label1: TLabel;
  14.     NdxEdit: TMaskEdit;
  15.     SpeedButton3: TSpeedButton;
  16.     Label2: TLabel;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure SpeedButton1Click(Sender: TObject);
  20.     procedure SpeedButton2Click(Sender: TObject);
  21.     procedure SpeedButton3Click(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.     EMemMap : TEFileMap;
  25.     CurPage : Cardinal;
  26.   public
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.DFM}
  36. Const
  37.       MaxArrSize = 16384;
  38. Type
  39.      IntArr = Array[0..MaxArrSize-1] Of Integer;
  40.      IntArrPtr = ^IntArr;
  41.  
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. Var
  44.    I,J     : Integer;
  45.    IArr    : IntArrPtr;
  46.    F       : File;
  47. begin
  48.   EMemMap:=TEFileMap.Create(Self);
  49.   If NOT EMemMap.CreateMutex('DAVESMUTEXF') then
  50.     EMemMap.RaiseMappingException;
  51.   AssignFile(F,'DAVEJUNK.DAT');
  52.   If NOT FileExists('DAVEJUNK.DAT') then
  53.   begin
  54.     New(IArr);
  55.     Rewrite(F,1);
  56.     For I:=1 to 3 do
  57.     begin
  58.       For J:=1 to MaxArrSize do
  59.         IArr^[J-1]:=J+((I-1)*MaxArrSize);
  60.       BlockWrite(F,IArr^[0],SizeOf(IntArr));
  61.     end;
  62.     Dispose(IArr);
  63.   end
  64.   else
  65.     Reset(F,1);
  66.   EMemMap.MemSize:=FileSize(F);
  67.   CloseFile(F);
  68.   Caption:='Int Array [0..'+IntToStr(MaxArrSize*3-1)+']';
  69.   If NOT EMemMap.MapExisting('DAVESMAPF',SizeOf(IntArr)) then
  70.     If NOT EMemMap.FCreateMemMap('DAVEJUNK.DAT','DAVESMAPF',SizeOf(IntArr)) then
  71.       EMemMap.RaiseMappingException;
  72.   CurPage:=0;
  73. end;
  74.  
  75. procedure TForm1.FormDestroy(Sender: TObject);
  76. begin
  77.   EMemMap.Free;
  78. end;
  79. procedure TForm1.SpeedButton1Click(Sender: TObject);
  80. Var
  81.     AnInt : Integer;
  82. begin
  83.   AnInt:=StrToInt(NdxEdit.Text);
  84.   If (AnInt>=0) AND (AnInt<MaxArrSize) then
  85.   begin
  86.     EMemMap.EnterCriticalSection;
  87.     Try
  88.       AnInt:=IntArrPtr(EMemMap.MemMap)^[AnInt];
  89.     Finally
  90.       EMemMap.LeaveCriticalSection;
  91.     end;
  92.     MessageDlg(IntToStr(AnInt),mtInformation,[mbOk],0);
  93.   end;
  94. end;
  95.  
  96. procedure TForm1.SpeedButton2Click(Sender: TObject);
  97. Var
  98.     Ndx    : Integer;
  99.     AnInt  : Integer;
  100.     AValue : String;
  101. begin
  102.   Ndx:=StrToInt(NdxEdit.Text);
  103.   If (Ndx>=0) AND (Ndx<MaxArrSize) then
  104.   begin
  105.     EMemMap.EnterCriticalSection;
  106.     Try
  107.       AnInt:=IntArrPtr(EMemMap.MemMap)^[Ndx];
  108.       AValue:=IntToStr(AnInt);
  109.       InputQuery('Get Value','NewValue',AValue);
  110.       AnInt:=StrToInt(AValue);
  111.       IntArrPtr(EMemMap.MemMap)^[Ndx]:=AnInt;
  112.     Finally
  113.       EMemMap.LeaveCriticalSection;
  114.     end;
  115.   end;
  116. end;
  117.  
  118.  
  119. procedure TForm1.SpeedButton3Click(Sender: TObject);
  120. Var
  121.    APage : String;
  122.    AInt  : Integer;
  123. begin
  124.   APage:=IntToStr(CurPage);
  125.   If InputQuery('Get Next Page (0..'+IntToStr(EMemMap.MaxSeeks)+')','Page Number',APage) then
  126.   begin
  127.     AInt:=StrToInt(APage);
  128.     If AInt<>Curpage then
  129.     begin
  130.       CurPage:=AInt;
  131.       If NOT EMemMap.Seek(CurPage) then
  132.         EMemMap.RaiseMappingException;
  133.     end;
  134.   end;
  135. end;
  136.  
  137. end.
  138.